﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Maticsoft.Common;

using System.Text;
namespace Golf.WebUI.AdminShow.CommoditiesMmanagement
{
    public partial class Golf_ProductAdd : Golf.WebUI.App_code.ValidateLogin
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Object obj = CookiesHelper.GetCookie("_AdminID");
                txtAdminId.Text = obj.ToString();
                txtAdminId.ReadOnly = true;
                BindTypeName();
                BindPinPai();
                txtPromotionStartTime.Text = DateTime.Now.ToString();
                txtPromotionEndTime.Text = DateTime.Now.ToString();
                txtPro_AddTime.Text = DateTime.Now.ToString();
            }
        }

        /// <summary>
        /// 绑定产品类型数据
        /// </summary>
        public void BindTypeName()
        {
            Golf.BLL.Golf_ProductType bll = new Golf.BLL.Golf_ProductType();
            DropDownList1.DataSource = bll.GetList("IsParent='false'");//绑定不父类的产品类型
            DropDownList1.DataBind();
            DropDownList1.Items.Insert(0, new ListItem() { Value = "0", Text = "--请选择--" });
        }

        /// <summary>
        /// 绑定产品 品牌
        /// </summary>
        public void BindPinPai()
        {
            Golf.BLL.Golf_BrandType bll = new Golf.BLL.Golf_BrandType();
            DropDownList2.DataSource = bll.GetList("IsDisplay=1");//绑定 数据库中显示的产品品牌
            DropDownList2.DataBind();
            DropDownList2.Items.Insert(0, new ListItem() { Value = "0", Text = "--请选择--" });
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {

            string strErr = "";
            if (DropDownList1.SelectedValue == "0")
            {
                strErr += "请选择产品 分类！\\n";
            }

            if (DropDownList2.SelectedValue == "0")
            {
                strErr += "请选择品牌！\\n";
            }
            if (this.txtPro_Name.Text.Trim().Length == 0)
            {
                strErr += "产品名称不能为空！\\n";
            }
            if (this.txtPro_Number.Text.Trim().Length == 0)
            {
                strErr += "商品编号不能为空！\\n";
            }
            if (!PageValidate.IsNumber(txtPro_BuyNum.Text))
            {
                strErr += "购买数量格式错误！\\n";
            }
            //if (this.txtPro_Picture.Text.Trim().Length == 0)
            //{
            //    strErr += "产品默认图 160-160不能为空！\\n";
            //}
            //if (this.txtPro_PicSmall.Text.Trim().Length == 0)
            //{
            //    strErr += "300-300不能为空！\\n";
            //}
            //if (this.txtPro_PicBig.Text.Trim().Length == 0)
            //{
            //    strErr += "800-800不能为空！\\n";
            //}
            if (this.txtPro_SeoTitle.Text.Trim().Length == 0)
            {
                strErr += "seo标题不能为空！\\n";
            }
            if (!PageValidate.IsNumber(txtPro_Dexterity.Text))
            {
                strErr += "左手/右手 1左手 0右手格式错误！\\n";
            }
            if (this.txtPro_Wood.Text.Trim().Length == 0)
            {
                strErr += "产品号数不能为空！\\n";
            }
            if (this.txtPro_Flex.Text.Trim().Length == 0)
            {
                strErr += "产品硬度不能为空！\\n";
            }
            if (this.txtPro_Shaft.Text.Trim().Length == 0)
            {
                strErr += "杆身不能为空！\\n";
            }
            if (!PageValidate.IsDecimal(txtPro_MarketPrice.Text))
            {
                strErr += "市场价格式错误！\\n";
            }
            if (!PageValidate.IsDecimal(txtPro_Price.Text))
            {
                strErr += "会员价格式错误！\\n";
            }
            if (!PageValidate.IsDecimal(txtPro_PromotionPrice.Text))
            {
                strErr += "促销价格式错误！\\n";
            }
            if (!PageValidate.IsDateTime(txtPromotionStartTime.Text))
            {
                strErr += "促销开始时间格式错误！\\n";
            }
            if (!PageValidate.IsDateTime(txtPromotionEndTime.Text))
            {
                strErr += "促销结束时间格式错误！\\n";
            }
            if (this.FCKeditor_Pro_Usage.Value.Trim().Length == 0)
            {
                strErr += "产品使用方法不能为空！\\n";
            }
            if (this.FCKeditor_Pro_Introduction.Value.Trim().Length == 0)
            {
                strErr += "产品介绍不能为空！\\n";
            }
            if (!PageValidate.IsDateTime(txtPro_AddTime.Text))
            {
                strErr += "产品添加时间格式错误！\\n";
            }
            if (!PageValidate.IsNumber(txtLimitBuyNum.Text))
            {
                strErr += "限制购买时间格式错误！\\n";
            }
            if (!PageValidate.IsNumber(txtInventoryNum.Text))
            {
                strErr += "库存时间格式错误！\\n";
            }
            if (!PageValidate.IsNumber(txtPro_CommentNum.Text))
            {
                strErr += "产品评论数格式错误！\\n";
            }
            if (!PageValidate.IsNumber(txtAdminId.Text))
            {
                strErr += "添加产品管理人id	Golf_格式错误！\\n";
            }

            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            int Pro_TypeId = int.Parse(DropDownList1.SelectedValue);
            int Pro_BrandTypeId = int.Parse(DropDownList2.SelectedValue);
            string Pro_Name = this.txtPro_Name.Text;
            string Pro_Number = this.txtPro_Number.Text;
            int Pro_BuyNum = int.Parse(this.txtPro_BuyNum.Text);

            //默认图片
            string Pro_Picture = this.FileName.Value;
            string Pro_PicSmall = this.FileName1.Value;
            //获取隐藏域的值
            string Pro_PicBig = this.FileName2.Value;

            StringBuilder sb_Pro_Picture = new StringBuilder();
            StringBuilder sb_Pro_PicSmall = new StringBuilder();
            StringBuilder sb_Pro_PicBig = new StringBuilder();

            string[] picture = hid_img_ifram.Value.Split('|');//获取传入的文件名
            foreach (var item in picture)
            {
                string[] p_one = item.Split(',');
                sb_Pro_Picture.Append(p_one[0] + ",");
                sb_Pro_PicSmall.Append(p_one[1] + ",");
                sb_Pro_PicBig.Append(p_one[2] + ",");
            }
            if (sb_Pro_Picture.Length != 0 && sb_Pro_PicSmall.Length != 0 && sb_Pro_PicBig.Length != 0)
            {
                Pro_Picture = sb_Pro_Picture.ToString().Substring(0, sb_Pro_Picture.Length - 1);
                Pro_PicSmall = sb_Pro_PicSmall.ToString().Substring(0, sb_Pro_PicSmall.Length - 1);
                Pro_PicBig = sb_Pro_PicBig.ToString().Substring(0, sb_Pro_PicBig.Length - 1);
            }

            string Pro_SeoTitle = this.txtPro_SeoTitle.Text;
            int Pro_Dexterity = int.Parse(this.txtPro_Dexterity.Text);
            string Pro_Wood = this.txtPro_Wood.Text;
            string Pro_Flex = this.txtPro_Flex.Text;
            string Pro_Shaft = this.txtPro_Shaft.Text;
            decimal Pro_MarketPrice = decimal.Parse(this.txtPro_MarketPrice.Text);
            decimal Pro_Price = decimal.Parse(this.txtPro_Price.Text);
            bool IsPromotion = this.chkIsPromotion.Checked;
            decimal Pro_PromotionPrice = decimal.Parse(this.txtPro_PromotionPrice.Text);
            DateTime PromotionStartTime = DateTime.Parse(this.txtPromotionStartTime.Text);
            DateTime PromotionEndTime = DateTime.Parse(this.txtPromotionEndTime.Text);
            string Pro_Usage = this.FCKeditor_Pro_Usage.Value;
            string Pro_Introduction = this.FCKeditor_Pro_Introduction.Value;
            bool IsShow = this.chkIsShow.Checked;
            bool isCommend = this.chkisCommend.Checked;
            DateTime Pro_AddTime = DateTime.Parse(this.txtPro_AddTime.Text);
            int LimitBuyNum = int.Parse(this.txtLimitBuyNum.Text);
            int InventoryNum = int.Parse(this.txtInventoryNum.Text);
            bool IsStock = this.chkIsStock.Checked;
            int Pro_CommentNum = int.Parse(this.txtPro_CommentNum.Text);
            int AdminId = int.Parse(this.txtAdminId.Text);
            bool IsGrounding = this.chkIsGrounding.Checked;

            Golf.Model.Golf_Product model = new Golf.Model.Golf_Product();
            model.Pro_TypeId = Pro_TypeId;
            model.Pro_BrandTypeId = Pro_BrandTypeId;
            model.Pro_Name = Pro_Name;
            model.Pro_Number = Pro_Number;
            model.Pro_BuyNum = Pro_BuyNum;
            model.Pro_Picture = Pro_Picture;
            model.Pro_PicSmall = Pro_PicSmall;
            model.Pro_PicBig = Pro_PicBig;
            model.Pro_SeoTitle = Pro_SeoTitle;

            model.Pro_Wood = Pro_Wood;

            model.Pro_Save = int.Parse(Pro_Shaft);
            model.Pro_MarketPrice = Pro_MarketPrice;
            model.Pro_Price = Pro_Price;
            model.IsPromotion = IsPromotion;
            model.Pro_PromotionPrice = Pro_PromotionPrice;
            model.PromotionStartTime = PromotionStartTime;
            model.PromotionEndTime = PromotionEndTime;
            model.Pro_Usage = Pro_Usage;
            model.Pro_Introduction = Pro_Introduction;
            model.IsShow = IsShow;
            model.isCommend = isCommend;
            model.Pro_AddTime = Pro_AddTime;
            model.LimitBuyNum = LimitBuyNum;
            model.InventoryNum = InventoryNum;
            model.IsStock = IsStock;
            model.Pro_CommentNum = Pro_CommentNum;
            model.AdminId = AdminId;
            model.IsGrounding = IsGrounding;

            Golf.BLL.Golf_Product bll = new Golf.BLL.Golf_Product();
            if (bll.Add(model) > 0)
            {
                Page.ClientScript.RegisterStartupScript(typeof(Page), "", "handler('添加成功！');", true);
            }
            else
            {

                Page.ClientScript.RegisterStartupScript(typeof(Page), "", "ymPrompt.errorInfo('保存失败');", true);
            }

        }


        public void btnCancle_Click(object sender, EventArgs e)
        {
            Page.ClientScript.RegisterStartupScript(typeof(Page), "", "handler(' 取消操作成功！');", true);
        }
        public string SaveImg(string img_Name, string Path)
        {
            int i = img_Name.LastIndexOf(".");
            string unit = img_Name.Substring(i);
            string imgSrc = img_Name.Substring(0, i);

            string imgSrc_small = imgSrc + "_small" + unit;
            string imgSrc_big = imgSrc + "_big" + unit;
            ThumNail.MakeThumNail(Path, Server.MapPath(@"/ImageProducts") + "\\" + img_Name, 160, 160, "HW");
            ThumNail.MakeThumNail(Path, Server.MapPath(@"/ImageProducts") + "\\" + imgSrc_small, 300, 300, "HW");
            ThumNail.MakeThumNail(Path, Server.MapPath(@"/ImageProducts") + "\\" + imgSrc_big, 800, 800, "HW");
            return img_Name + "," + imgSrc_small + "," + imgSrc_big;
        }
    }
}